package com.cms.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cms.domain.Data;
import com.cms.domain.Item;
import com.cms.mapper.DataDao;
import com.cms.service.DataService;


@Service
public class DataServiceImpl implements DataService{

	@Autowired
	private DataDao entityDao;
	
	public Data getById(Long id){
		return entityDao.getById(id);
	}
	
	public Data getByIds(Long id_sbj,Long rowNum,Long id_item){
		return entityDao.getByIds(id_sbj, rowNum, id_item);
	}

	public List<Data> getList(Long id_sbj){
		return entityDao.getList(id_sbj);
	}
	
	public List<Data> getList(Long id_sbj, int startIndex, int size){
		return entityDao.getListForPage(id_sbj, startIndex, size);
	}
	
	public List<Data> getRowList(Long id_sbj, Long rowNum){
		return entityDao.getRowList(id_sbj,rowNum);
	}
	
	public void add(Data entity){
		entityDao.add(entity);
	}

	public void update(Data entity){
		entityDao.update(entity);
	}

	public void delete(Data entity){
		entityDao.delete(entity);
	}
	
	public Long getRowNumMax(Long id_sbj){
		return entityDao.getRowNumMax(id_sbj);
	}

	public int getRowCount(Long id_sbj){
		int rowCount = 0;
		List<Map> count_list = entityDao.getRowCount(id_sbj,null);
		if(count_list!=null && count_list.size()!=0){
			rowCount = count_list.size();
		}
		return rowCount;
	}
	

//	public int getRowCount(Long id_sbj, Map<String, Item> item_map, Map<String, Object> params){
//		int rowCount = 0;
//		
//		StringBuffer condition_buf = new StringBuffer();
//		
//		for(Map.Entry entry : params.entrySet()){
//			String item_name = entry.getKey().toString();
//			Object value = entry.getValue();
//			
//			Item item = item_map.get(item_name);
//			if(item!=null){
//				if(item.getType().equals("blob")){
//					condition_buf.append(" OR ( id_item = '".concat(item.getId().toString()).concat("' AND valueBlob "));
//				}else{
//					condition_buf.append(" OR ( id_item = '".concat(item.getId().toString()).concat("' AND `value` "));
//				}
//				if(value instanceof Integer || value instanceof BigDecimal || value instanceof Long || value instanceof Double ){
//					condition_buf.append(" = '".concat(value.toString()).concat("' ) "));
//				}else{
//					condition_buf.append(" like %'".concat(value.toString()).concat("%' ) "));
//				}
//			}
//		}
//		
//		String condition = condition_buf.toString();
//		if(condition!=null && condition.startsWith(" OR ")){
//			condition = condition.substring(3);
//		}
//		
//		List<Map> count_list = entityDao.getRowCount(id_sbj,condition);
//		
//		for(Map count_map:count_list){
//			Integer count = Integer.parseInt(count_map.get("DATA_COUNT").toString());
//			if(count>=params.size()){
//				rowCount++;
//			}
//		}
//		
//		return rowCount;
//	}
	
	public List<Map> getRowCountMap(Long id_sbj){
		HashMap condition_map= new HashMap();
		condition_map.put("id_sbj", id_sbj);
		return entityDao.getRowCount(id_sbj,null);
	}
	
	public List<Map> getRowCountMap(Long id_sbj, Map<String, Item> item_map, Map<String, Object> params){
		HashMap condition_map= new HashMap();
		
		StringBuffer condition_buf = new StringBuffer();
		
		for(Map.Entry entry : params.entrySet()){
			String item_name = entry.getKey().toString();
			Object value = entry.getValue();
			
			Item item = item_map.get(item_name);
			if(item!=null){
				if(item.getType().equals("blob")){
					condition_buf.append(" OR ( id_item =".concat(item.getId().toString()).concat(" AND valueBlob "));
				}else{
					condition_buf.append(" OR ( id_item =".concat(item.getId().toString()).concat(" AND `value` "));
				}
				if(value instanceof Integer || value instanceof BigDecimal || value instanceof Long || value instanceof Double ){
					condition_buf.append(" = '".concat(value.toString()).concat("' ) "));
				}else{
					condition_buf.append(" like '%".concat(value.toString()).concat("%' ) "));
				}
			}
		}
		
		String condition = condition_buf.toString();
		if(condition!=null && condition.startsWith(" OR ")){
			condition = condition.substring(3);
		}
		
		condition_map.put("id_sbj", id_sbj);
		return entityDao.getRowCount(id_sbj,condition);
	}
	
}

